const {createBlocks, getBlocksInfo, updateBlocksInfo} = require('../service/blocks.service');

const {createLog} = require('../service/log.service');

const {databaseError} = require('../constant/err.type');

class BlocksController {

    async addBlocks(ctx, next) {
        let {blockId, blockName, blockDesc, price, blockType, pileNumber, blockPath, relationItemId, relationItemType, totalGrowDay, repeatPickDay, statusUI, composition} = ctx.request.body;
        // 操作数据库
        try {
            let res = await createBlocks(blockId, blockName, blockDesc, price, blockType, pileNumber, blockPath, relationItemId, relationItemType, totalGrowDay, repeatPickDay, statusUI, composition);
            // 记录日志
            await createLog(ctx, `添加方块成功，方块名：${blockName}`);
            ctx.body = {
                code: 1,
                message: '方块添加成功！',
                result: {
                    id: res.id,
                    blockName: res.blockName
                }
            };
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx)
        }
    }

    async updateBlocks(ctx, next) {
        // 获取数据
        let {id, blockId, blockName, blockDesc, price, blockType, pileNumber, blockPath, relationItemId, relationItemType, totalGrowDay, repeatPickDay, statusUI, composition} = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateBlocksInfo({
                id,
                blockId,
                blockName,
                blockDesc,
                price,
                blockType,
                pileNumber,
                blockPath, relationItemId, relationItemType, totalGrowDay, repeatPickDay, statusUI,
                composition
            });
            if (res) {
                // 记录日志
                await createLog(ctx, `修改方块成功，方块名：${blockName}`);
                ctx.body = {
                    code: 1,
                    message: '方块修改成功！',
                    result: {}
                }
            } else {
                ctx.app.emit('error', databaseError, ctx);
            }
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }

    async queryBlocks(ctx, next) {

        let {id, blockName, blockType, blockDesc, flag, pageInfo} = ctx.request.query;
        try {
            let res = await getBlocksInfo({id, blockName, blockType, blockDesc, flag, pageInfo});
            ctx.body = {
                code: 1,
                message: '方块查询成功！',
                data: res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }

    async queryBlocksDetail(ctx, next) {
        let {id} = ctx.request.query;
        try {
            let res = await getBlocksInfo({id});
            ctx.body = {
                code: 1,
                message: '方块详情查询成功！',
                result: res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }

    async deleteBlocks(ctx, next) {
        // 获取数据
        let {id, flag} = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateBlocksInfo({id, flag});
            if (res) {
                // 记录日志
                await createLog(ctx, `删除方块成功，方块ID:${id}`);
                ctx.body = {
                    code: 1,
                    message: '方块删除成功！',
                    result: {}
                }
            } else {
                ctx.app.emit('error', databaseError, ctx);
            }
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }
}

module.exports = new BlocksController();